// 2025/09/23
// Author: Forever
#include <bits/stdc++.h>
#define int long long
using namespace std;

int n, m, st, ed, k, nfish;
vector<int> fish[21];

class Matrix {
public:
    int n, m, a[51][51];
    Matrix()
    {
        n = m = 0;
    }
    Matrix(int N, int M)
    {
        n = N, m = M;
        memset(a, 0, sizeof(a));
    }
    Matrix operator*(Matrix b)
    {
        Matrix c(n, b.m);
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= b.m; j++)
                for (int k = 1; k <= m; k++)
                    c[i][j] += a[i][k] * b[k][j], c[i][j] %= 10000;
        return c;
    }
    int* operator[](int x)
    {
        return a[x];
    }
};

Matrix quick_pow(Matrix a, int k)
{
    Matrix ans(a.n, a.n);
    for (int i = 1; i <= a.n; i++)
        ans.a[i][i] = 1;
    while (k) {
        if (k & 1)
            ans = ans * a;
        a = a * a;
        k >>= 1;
    }
    return ans;
}

signed main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin >> n >> m >> st >> ed >> k;
    st++;
    ed++;
    Matrix normal(n, n), t[12], mat(n, n);
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        u++;
        v++;
        normal[u][v] = 1;
        normal[v][u] = 1;
    }
    cin >> nfish;
    for (int i = 1; i <= nfish; i++) {
        int x, p;
        cin >> x;
        for (int j = 1; j <= x; j++) {
            cin >> p;
            p++;
            fish[i].push_back(p);
        }
    }
    for (int i = 1; i <= n; i++)
        mat[i][i] = 1;
    for (int i = 0; i < 12; i++) {
        t[i] = normal;
        for (int j = 1; j <= nfish; j++)
            for (int p = 1; p <= n; p++)
                t[i][p][fish[j][i % fish[j].size()]] = 0;
        mat = t[i] * mat;
    }
    Matrix ans = quick_pow(mat, k / 12);
    for (int i = 0; i < k % 12; i++)
        ans = t[i] * ans;
    Matrix res(n, 1);
    res[st][1] = 1;
    res = ans * res;
    cout << res[ed][1] << endl;
    return 0;
}